package com.yss.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yss.common.core.constant.SecurityConstants;
import com.yss.common.core.domain.YssResponse;
import com.yss.common.core.enums.YssYseNoEnum;
import com.yss.common.core.utils.StringUtils;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.security.utils.SecurityUtils;
import com.yss.reportworld.api.RemoteStatisticReportService;
import com.yss.system.api.domain.SysNotice;
import com.yss.system.service.ISysNoticeService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 平台首页统计项
 */
@RestController
@Slf4j
@Api(tags = "平台首页统计项管理api")
@RequestMapping("/statistics")
public class StatisticsController extends BaseController {
    @Autowired
    private ISysNoticeService sysNoticeService;
    @Autowired
    private RemoteStatisticReportService remoteStatisticReportService;


    /**
     * 版本号
     *
     * @return
     */
    @GetMapping("/getVersionList")
    public AjaxResult getVersionList() {
        YssResponse<List<String>> response = remoteStatisticReportService.getVersionList(SecurityConstants.INNER);
        if (StringUtils.isNotEmpty(response.getData())) {
            return success(response.getData());
        }
        return success();
    }

    @GetMapping("/getCount")
    public AjaxResult getCount() {
        YssResponse<Map<String, Long>> response = remoteStatisticReportService.getCount(SecurityConstants.INNER);
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isEmpty(response.getData())) {
            return ajax;
        }
        Map<String, Long> map = response.getData();
        if (StringUtils.isNotEmpty(map)) {
            ajax.putAll(map);
        }
        return ajax;
    }

    @GetMapping("/getCountVersion")
    public AjaxResult getCountVersion() {
        YssResponse<Map<String, List<Long>>> response = remoteStatisticReportService.getCountVersion(SecurityConstants.INNER);
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isEmpty(response.getData())) {
            return ajax;
        }
        Map<String, List<Long>> map = response.getData();
        if (StringUtils.isNotEmpty(map)) {
            ajax.putAll(map);
        }
        return ajax;
    }

    /**
     * 首页公告信息
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/getNoticeInfo")
    public AjaxResult getNoticeInfo() {
        AjaxResult ajax = AjaxResult.success();
        // 系统公告信息
        QueryWrapper<SysNotice> wrapper = new QueryWrapper<>();
        wrapper.eq("STATUS", YssYseNoEnum.YES.getCode());
        List<SysNotice> sysNoticeList = sysNoticeService.list(wrapper);
        List<SysNotice> sysNoticeListNew = new ArrayList<>();
        if (StringUtils.isNotEmpty(sysNoticeList)) {
            for (SysNotice notice : sysNoticeList) {
                if (StringUtils.isNull(notice)) {
                    continue;
                }
                // 公共配置了角色，就按照角色添加
                if (StringUtils.isNotEmpty(notice.getRoleList()) && !SecurityUtils.isAdmin(SecurityUtils.getUserId())) {
                    boolean falg = false;
                    for (String roleKey : SecurityUtils.getLoginUser().getRoles()) {
                        if (!notice.getRoleList().contains(roleKey)) {
                            falg = false;
                            continue;
                        }
                        falg = true;
                        break;
                    }
                    if (!falg) {
                        continue;
                    }
                }
                if ("2".equals(notice.getNoticeType())) {
                    notice.setNoticeType("info");
                    continue;
                }
                notice.setNoticeType("success");
                sysNoticeListNew.add(notice);
            }
        }
        ajax.put("systemInfoNotice", sysNoticeList);
        return ajax;
    }
}
